var api = require('../../utils/api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list:[]
    },

    onLoad(options) {

    },

    onShow() {
        this.getlist();
    },
    getlist(){
        var that=this;
        api.post({
            url: 'wxapp/user/getcasexm',
            data: {},
            success: data => {
                that.setData({
                    list:data.data
                });
            }
        })
    },
    addxm(e) {
        var that=this;
        wx.showModal({
            editable: true,
            title: '添加任务',
            placeholderText: '请输入任务名称',
            success: res => {
                if (res.confirm) {
                    var title=res.content;

                    api.post({
                        url: 'wxapp/user/addcasexm',
                        data: {title:title},
                        success: data => {
                            that.getlist();
                        }
                    })
                } else {
                    console.log('用户点击了取消')
                }
            }
        })
    },

    edites(ret) {
        console.log(ret);
        var index=ret.currentTarget.dataset.index;
        var list=this.data.list;
        var id=list[index].id;
        var title=list[index].title;
        var that=this;
        wx.showModal({
            editable: true,
            title: '修改任务',
            content: title,
            success: res => {
                if (res.confirm) {
                    var title=res.content;
                    api.post({
                        url: 'wxapp/user/setcasexm',
                        data: {id:id,title:title},
                        success: data => {
                            that.getlist();
                        }
                    })
                } 
            }
        })


    },
    delete(ret){
        console.log(ret);
        var id=ret.currentTarget.dataset.id;
        var that=this;
        wx.showModal({
            title: '删除',
            content: '确定要删除该项目吗?',
            success: res => {
                if (res.confirm) {
                    var title=res.content;
                    api.post({
                        url: 'wxapp/user/deletecasexm',
                        data: {id:id},
                        success: data => {
                            that.getlist();
                        }
                    })
                } 
            }
        })

    },
    select(ret){
        var index=ret.currentTarget.dataset.index;
        var list=this.data.list;
        var id=list[index].id;
        var title=list[index].title;
        var that=this;
        var select=list[index].select;

        if(select==1){
            list[index].select=0;
        }else{
            list[index].select=1;
        }

        this.setData({
            list:list
        })

    },

    submits(){

        var list=this.data.list;

        var str='';

        list.forEach(function(item){
            if(item.select==1){
                str+=item.id+',';
            }
        })
        console.log(str);
        if(!str){
            wx.showToast({
              title: '请选择',
              icon:'none'
            })
            return false;
        }

        getApp().globalData.xx =str;
        wx.navigateBack({
          delta: 1,//返回一级
        })


    }

})